<?php namespace App\Http\Controllers\user\uc;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use Input;
use File;
use App\Models\Area;
use Uuid;
use App\Models\InvestmentType;
use App\Models\EnergyTypes;
use App\Models\Domains;
use App\Models\Techs;
use App\Models\Devices;
use App\Models\Technologies;
use App\Models\Projects;


class CommonController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		//
	}

	public function getAllProject()
	{
		$data = Projects::where("verify_state" ,1)->get()->toArray();
		return $this->json(["data" => $data]);
	}

	public function getArea()
	{
		$pid = Input::get('pid', 1);
		$area = Area::where('parent_id', $pid)->get();
		return $this->json(['area'=>$area]);
	}

	public function uploadImg()
	{
		$file = Input::file("imgFile");
		// $name = Input::get('name');
		if(!is_null($file) && $file->isValid()){
			$ext = $file->getClientOriginalExtension();
			// if($ext != "jpg" && $ext != "jpeg" && $ext != "png"){
			// 	return self::error(self::FILE_TYPE_IS_WRONG);
			// }
			$newName = Uuid::generate() . "." . $ext;
			$path = base_path() . "/public/upload";
			$file->move($path, $newName);
			chmod($path . "/" . $newName, 0777);

			return $this->json([
				"filename"	=>	$newName
				// 'name'	=>	$name
			]);
		}
	}

	public function getInvestmentType()
	{
		$data = InvestmentType::get()->toArray();
		return $this->json(['data' => $data]);
	}

	public function getEnergyType()
	{
		$data = EnergyTypes::get()->toArray();
		return $this->json(['data' => $data]);
	}

	public function getDomainType()
	{
		$parent_id = Input::get("parent_id", 0);
		if($parent_id == 0)
		{
			return;
		}
		$model = Domains::where("parent_id", $parent_id);
		if($model->count())
		{
			$data = $model->get()->toArray();
			return $this->json(["data" => $data]);
		}
	}

	public function getDomains()
	{
		$parent_id = Input::get("parent_id", 0);
		$model = Domains::where("parent_id", $parent_id);
		$data = $model->get()->toArray();
		return $this->json(["data" => $data]);
	}

	public function getTechs()
	{
		$data = Techs::all()->toArray();
		return $this->json(["data" => $data]);
	}

	public static function getAllProvince()
	{
		$province = Area::where("parent_id", 1)->get();
		return $province;
	}

	public function getUseDevice()
	{
		$data = Devices::all()->toArray();
		return $this->json(["data" => $data]);
	}

	public function getUseTech()
	{
		$data = Technologies::all()->toArray();
		return $this->json(["data" => $data]);
	}


	public function projectShow()
	{
		$pid = Input::get("pid");
		$model = Projects::where("id", $pid);
		if($model->count()){
			$verify_state = $model->pluck("verify_state");
			if($verify_state == 0){
				return self::error(self::IS_AUDITING);
			}else if($verify_state == 2){
				return self::error(self::AUDIT_NOT_PASS);
			}else{
				return $this->json([]);
			}
		}
	}

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}

}
